// pages/group/detail.js
import {getgdetail,outgroup,ingroup} from "../../utils/api"
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  editgropu(e){
    var id=e.target.dataset.id
    wx.navigateTo({
      url: '/pages/group/eidt?gid='+id,
    })
 },
 logs(){
  var that=this
  wx.navigateTo({
    url: '/pages/glogs/glogs?gid='+that.data.gid,
  })
 },
 baoshu(){
   var that=this
  wx.navigateTo({
    url: '/pages/baoshu/baoshu?gid='+that.data.gid,
  })
 },
  ziliao(e){
     var id=e.target.dataset.id
     wx.navigateTo({
       url: '/pages/ziliao/ziliao?gid='+id,
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that=this;
    var gid=options.id;
   
    getgdetail(gid).then((res)=>{
      console.log(res)
       if(res.code==1){
        that.setData({
            group:res.data.detail,
            isin:res.data.isin,
            isguan:res.data.isguan,
            users:res.data.users,
            gid:gid,
            pagefunc:options
        })
       }else{
         wx.showToast({
           title:res.msg,
           icon:"none"
         })
       }
    }) 
  },

  tousers(){
    var that=this
    wx.navigateTo({
      url: '/pages/group/users?gid='+that.data.gid,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  ingroups(){
    var that=this
    ingroup(that.data.gid).then(res=>{
      wx.showToast({
        title:res.msg,
        icon:'none'
      })
       if(res.code==1){
        that.onLoad(that.data.pagefunc)
       }

    })
  },
  outgroups(e){
    var that=this
    Dialog.confirm({
      title: '确认退出共修组吗',
      message: '退出后您在本组所修功德将被清空且无法恢复',
    })
      .then(() => {
        outgroup(that.data.gid).then(res=>{
          wx.showToast({
               title:res.msg,
               icon:'none'
             })
            if(res.code==1){
              wx.switchTab({
                url: '/pages/index/index',
              }) 
            }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
     return{
       title:this.data.group.name,
       desc:"好友邀请你加入共修组",
       path:"/pages/group/detail?id="+this.data.gid
     }
  }
  
})